<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

	class Admin_Model extends MY_Model{
		
		public $table;
		public $module;
		public $status_name = 'status';
		public $list_view_condition = '';
		public $list_view_searched_fields = array();
		public $list_view_searched_filter = array();
		public $form_view_fields = array();
		protected $checkExistField = array();
		protected $listUploadField = array();
		
		
		function getsearchContent($list_view_fields,$search_fields,$start=-1,$limit=-1) {
			if($this->module == 'accounts'){
				$this->db->select($this->table.'.*, '.ADMIN_GROUP_TABLE.'.name as group_name');
				$this->db->join(ADMIN_GROUP_TABLE, ADMIN_GROUP_TABLE.'.id='.$this->table.'.group_id','LEFT');
				if($this->session->userdata('userInfo')!='root'){
					$this->db->where('username != "root"');
				}
			} else {
				$this->db->select("{$this->table}.*");
				foreach($list_view_fields as $key => $field_config){
					if(!empty($field_config['select_config'])){
						$select_config = $field_config['select_config'];
						if(!empty($select_config['referred_table_name'])){
							$referred_table_name = $select_config['referred_table_name'];
							$referred_table_alias_name = $key.'_'.$referred_table_name;
							$referred_field_name = $select_config['referred_field_name'];
							$referred_select_field_name = $key.'_'.$referred_field_name;
							$this->db->join("{$referred_table_name} {$referred_table_alias_name}","{$this->table}.{$key} = {$referred_table_alias_name}.id",'left');
							$this->db->select("{$referred_table_alias_name}.{$referred_field_name} AS {$referred_select_field_name}");
						}
					}
				}
				if(!empty($this->list_view_condition)){
					$list_view_condition_arr = explode('AND',$this->list_view_condition);
					if(!empty($list_view_condition_arr)){
						foreach($list_view_condition_arr as $condition){
							$this->db->where($this->table.'.'.$condition);
						}
					}
				}
			}
			$this->getSearchCriteria($search_fields);
			if ($limit == -1) {
				$query = $this->db->from($this->table);
				$result = $this->db->count_all_results();
			} else {
				$order_by = $this->input->post('order_by');
				$func_order_by = $this->input->post('func_order_by');
				$this->db->limit($limit, $start);
				$this->db->order_by($this->table.'.'.$func_order_by, $order_by);
				$query = $this->db->get($this->table);
				$result = $query->result();
			}

			return $result;
		}
		
		function getSearchCriteria($fields){
			if($fields == 'all'){
				$fields = $this->list_view_searched_fields;
			} else {
				foreach($this->list_view_searched_fields as $field => $title){
					if($field == $fields){
						$fields = array($field	=>	$title);
					}
				}
			}
			
			$content = $this->input->post('content');
			$db = &$this->db;
			if ($content != '' && $content != SEARCH_TEXTBOX_MESSAGE) {
				if(is_array($fields) && count($fields) > 0){
					$tempCount = 0;
					$condition = '';
					$or_suffix = ' OR ';
					foreach($fields as $field => $title){
						$tempCount++;
						$condition .= "({$this->table}.{$field} LIKE '%{$content}%')".$or_suffix;
					}
					if(!empty($condition)){
						$suffix_length = strlen($or_suffix);
						$condition = substr($condition,0,-$suffix_length);
						$db->where("({$condition})");
					}
				}
			}

			if(!empty($this->list_view_searched_filter)){
				if(isset($this->list_view_searched_filter['sub_filter'])){
					//sub filter
					if($this->input->post('sub_filter_' . $this->list_view_searched_filter['sub_filter']['name']) != 'all')
						$db->where($this->list_view_searched_filter['sub_filter']['name']." = " . $this->input->post('sub_filter_' . $this->list_view_searched_filter['sub_filter']['name']) );
				}
				else{
					if($this->input->post('search_filter_' . $this->list_view_searched_filter['name']) != 'all')
						$db->where($this->list_view_searched_filter['name']." = " . $this->input->post('search_filter_' . $this->list_view_searched_filter['name']) );
				}
				
			}
			
			$dateFrom = $this->input->post('dateFrom');
			$dateTo = $this->input->post('dateTo');
			$dateFrom_formatted = convertVNDate($dateFrom);
			$dateTo_formatted = convertVNDate($dateTo);
			if ($dateFrom != '' && $dateTo == '') {
				$db->where("{$this->table}.created >= '{$dateFrom_formatted}'");
			}
			if ($dateFrom == '' && $dateTo != '') {
				$db->where("{$this->table}.created <= '{$dateTo_formatted}'");
			}
			if ($dateFrom != '' && $dateTo != '') {
				$db->where("{$this->table}.created >= '{$dateFrom_formatted}'");
				$db->where("{$this->table}.created <= '{$dateTo_formatted}'");
			}
		}
		
		function getTotalContent() {
			$content = $this->input->post('content');
			$dateTo = $this->input->post('dateTo');
			$dateFrom = $this->input->post('dateFrom');

			$this->db->select('*');
			if(!empty($this->list_view_condition)){
				$this->db->where($this->list_view_condition);
			}
			$query = $this->db->count_all_results($this->table);
			if ($query > 0) {
				return $query;
			} else {
				return FALSE;
			}

		}
		
		function getDetailManagement($id){
			$this->db->select('*');
			$this->db->where('id',$id);
			$query = $this->db->get($this->table);

			if($query->row()){
				return $query->row();
			}else{
				return false;
			}
		}
		
		function checkDataExist($field,$data){
			$this->db->select('id');
			$this->db->where($field,$data);
			$this->db->limit(1);
			$query = $this->db->get($this->table);

			if($query->result()){
				return true;
			}else{
				return false;
			}
		}
		
		function getVarModel($name){
			if(isset($this->$name)){
				$var_model = $this->$name;
				foreach($var_model as $key=>$value){
					unset($var_model[$key]);
					$var_model['input_'.$key] = $value;
				}
				return $var_model;
			} else {
				return FALSE;
			}
		}


	function admincp_checkDataExist($result=''){
		if(count($this->checkExistField > 0)){
			foreach($this->checkExistField as $k => $v){
				if($result != ''){
					if($result->$k != $this->input->post($v)){
						if($this->checkDataExist($k,$this->input->post($v))){
							print 'error-'.$k.'-exists';
							exit;
						}
					}
				} else {//pr('else');
					if($this->checkDataExist($k,$this->input->post($v))){last_query(1);
						print 'error-'.$k.'-exists';
						exit;
					}
				}
				
			}
		}
	}
	
	/*------------------------------------ Begin Admin GALLERY ------------------------------------*/
	function getListImageOfGallery($category_id, $table = ''){
		$this->db->select('id,image');
		$this->db->where('category_id',$category_id);
		$this->db->order_by('created','DESC');
		$query = $this->db->get(PREFIX.$table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getDetailImageOfGallery($id, $table = ''){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$table);

		if($query->row()){
			return $query->row();
		}else{
			return false;
		}
	}

	/*------------------------------------ End Admin GALLERY ------------------------------------*/
}